﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using Light.Utility.WinAPI;
using System.Runtime.InteropServices;

namespace Light.Utility
{
    public class Shell
    {
        
        public static void ExecCommand(string command)
        {
            ExecCommand(command, null, null);
        }

        public static void ExecCommand(string command, string arguments)
        {
            ExecCommand(command, arguments, null);
        }

        public static void ExecCommand(string command, string arguments, string workingDirectory)
        {
            if ((command == null) || (command.Trim().Length == 0))
            {
                throw new ArgumentException("无效的命令行。", "command");
            }
            ProcessStartInfo startInfo = new ProcessStartInfo(command)
            {
                UseShellExecute = true,
                ErrorDialog = true
            };
            if ((arguments != null) && (arguments.Trim().Length != 0))
            {
                startInfo.Arguments = arguments;
            }
            if ((workingDirectory != null) && (workingDirectory.Trim().Length != 0))
            {
                startInfo.WorkingDirectory = workingDirectory;
            }
            try
            {
                Process.Start(startInfo);
            }
            catch
            {
            }
        }

        public static Icon GetFileIcon(string fileName, IconSize size)
        {
            return GetFileIcon(fileName, size, false);
        }

        public static Icon GetFileIcon(string fileName, IconSize size, bool linkOverlay)
        {
            Shell32.SHFILEINFO psfi = new Shell32.SHFILEINFO();
            uint uFlags = 0x110;
            if (linkOverlay)
            {
                uFlags += 0x8000;
            }
            if (IconSize.Small == size)
            {
                uFlags++;
            }
            Shell32.SHGetFileInfo(fileName, 0x80, ref psfi, (uint)Marshal.SizeOf(psfi), uFlags);
            Icon icon = (Icon)Icon.FromHandle(psfi.hIcon).Clone();
            User32.DestroyIcon(psfi.hIcon);
            return icon;
        }

        public static Icon GetFolderIcon(FolderIconType folderIconType, IconSize size)
        {
            uint uFlags = 0x110;
            if (FolderIconType.Open == folderIconType)
            {
                uFlags += 2;
            }
            if (IconSize.Small == size)
            {
                uFlags++;
            }
            Shell32.SHFILEINFO psfi = new Shell32.SHFILEINFO();
            Shell32.SHGetFileInfo(null, 0x10, ref psfi, (uint)Marshal.SizeOf(psfi), uFlags);
            Icon icon = (Icon)Icon.FromHandle(psfi.hIcon).Clone();
            User32.DestroyIcon(psfi.hIcon);
            return icon;
        }

        public static void OpenFile(string fileName)
        {
            ExecCommand(fileName);
        }

        
        public enum FolderIconType
        {
            Open,
            Closed
        }

        public enum IconSize : uint
        {
            Large = 1,
            Small = 0
        }
    }



}
